<?php

/**
 * @Author: chenjianghui
 * @Date:   2018-08-02 11:40:50
 * @Last Modified by:   chenjianghui
 * @Last Modified time: 2018-10-12 17:48:07
 */
namespace app\common\queue;

use app\common\model\Message;
use app\common\model\Queue;
use \think\queue\Job;
use app\admin\controller\Base;
use JPush\Client;

class SendMessageQueueClient
{


    /**
     * 站内信
     * @Author   chenjianghui
     * @DateTime 2018-08-02T14:03:50+0800
     * @param    Job                      $job  [description]
     * @param    [type]                   $data [description]
     * @return   [type]                         [description]
     */
    public function instationMsg(Job $job, $data)
    {

        //print("<info>开始执行任务: ".var_export($data,true)."</info> \n");
        $isJobDone = $this->send_instationMsg($data);
        //dump($isJobDone);die;
        if ($isJobDone) {
            //成功删除任务
            $job->delete();
            //echo '执行任务成功:删除任务'.'</br>';
        } else {
            //任务轮询4次后删除
            if ($job->attempts() > 3) {
                // 第1种处理方式：重新发布任务,该任务延迟10秒后再执行
                //$job->release(10);
                // 第2种处理方式：原任务的基础上1分钟执行一次并增加尝试次数
                //$job->failed();
                // 第3种处理方式：删除任务
                $job->delete();
            }
        }
    }

    public function send_instationMsg($data)
    {

        $filename = ROOT_PATH . '/log/order_praise/' . date('Ymd') . '.log';

        $join[] = ["sd_shop_task k","k.task_id = r.task_id","left"];

        $shop_order_info = \think\Db::name("shop_order")->alias("r")->join($join)->where(["r.order_id"=>$data["message_send_id"]])->field("r.*,k.is_need_send,k.is_good_evalute,k.is_review")->find();

        $op = 0;

        if($shop_order_info["is_need_send"] == 1 && $shop_order_info["is_good_evalute"] == 2 && $shop_order_info["is_review"] == 2){

            \think\Db::name("shop_order")->where(["order_id"=>$data["message_send_id"]])->update(["status" => 13,"update_time"=>time(),"author"=>0,"author_status"=>1,"finished_time"=>time()]);//隔日单和精刷单--已完成

                //改变用户的完成订单数
            \think\Db::name("user_attribute")->where("uid",$data["user_id"])->setInc("task_finish_count",1);

                $openid = Db::name("wx_users")->where("uid",$data["user_id"])->value("openid");

                $content = [
                    $shop_order_info["shop_name"], $shop_order_info["goods_sno"], $shop_order_info["real_money"], $openid
                ];//推荐内容
                $msg = [
                    "condition" => 14,
                    "touser" => $openid,
                    "content" => $content
                ];
                $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                $MiniMsg->sendMsg();//发送短信模板订单已完成
                //计算钱
                $gold = new \app\common\controller\GoldBean();
                $gold->finishedBean($shop_order_info);

                $op = \think\Db::name("queue")->where("id",$data["id"])->update(["status"=>1]);


            }else{

            \think\Db::name("shop_order")->where('order_id',$data["message_send_id"])->update(['status' => 9,"update_time"=>time()]);

                $openid = \think\Db::name("wx_users")->where("uid",$data["user_id"])->value("openid");

                $content = [
                    $shop_order_info["order_sno"], date("Y-m-d H:i:s",$shop_order_info["create_time"]), $shop_order_info["real_money"], 1,"已发货"
                ];//推荐内容
                $msg = [
                    "condition" => 19,
                    "touser" => $openid,
                    "content" => $content
                ];
                $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                $MiniMsg->sendMsg();//发送短信模板订单已完成

            $op = \think\Db::name("queue")->where("id",$data["id"])->update(["status"=>1]);

            }
            if($op){

                $str = PHP_EOL.date('Y-m-d H:i:s', time()).'--订单编号:'.$shop_order_info["order_sno"].'que_id='.$data['id'].', 用户id = ' .$shop_order_info['uid'] .', res = succeess('. json_encode($shop_order_info) .')'.PHP_EOL;
                file_put_contents($filename, $str . PHP_EOL, FILE_APPEND);

            } else {
                $str = PHP_EOL.date('Y-m-d H:i:s', time()).'--订单编号:'.$shop_order_info["order_sno"].'que_id='.$data['id'].', 用户id = ' .$shop_order_info['uid'] .', res = succeess('. json_encode($shop_order_info) .')'.PHP_EOL;
                file_put_contents($filename, $str . PHP_EOL, FILE_APPEND);
            }
    }



}
